<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件


if(!function_exists('make_random_str')){
    /**
     * 随机字符
     * @param int $length 长度
     * @param string $type 类型
     * @param int $convert 转换大小写
     * @return string
     */
    function make_random_str($length=6, $type='string', $convert=0){

        $config = array(
            'number'=>'1234567890',
            'letter'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
            'string'=>'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789',
            'all'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
        );

        if(!isset($config[$type])) $type = 'string';
        $string = $config[$type];

        $code = '';
        $strlen = strlen($string) -1;
        for($i = 0; $i < $length; $i++){
            $code .= $string{mt_rand(0, $strlen)};
        }
        if(!empty($convert)){
            $code = ($convert > 0)? strtoupper($code) : strtolower($code);
        }
        return $code;

    }
}

if(!function_exists('check_login')){
    function check_login($token){
        $user = cache($token);
        if(!$user){
            return false;
        }

        //检查是否过期
        $oldToken = cache($user->openid);
        if($oldToken != $token){
            return false;
        }

        return $user;
    }
}


if(!function_exists('get_encrypt_param')){
    function get_encrypt_param(){
        $param = [
            'method'=>'AES-256-CBC',
            'iv'=>'1234567890123456',
            'key'=>'12345',
        ];
        return $param;
    }
}

if(!function_exists('make_token')){
    /**
     * @param $data array
     * @return string
     */
    function make_token($data){
        $data = json_encode($data);
        $param = get_encrypt_param();
        $encode = openssl_encrypt($data,$param['method'],$param['key'],0,$param['iv']);
        return $encode;
    }
}

if(!function_exists('decode_token')){
    /**
     * @param $string string
     * @return string
     */
    function decode_token($string){
        $param = get_encrypt_param();
        $decode = openssl_decrypt($string,$param['method'],$param['key'],0,$param['iv']);
        return $decode;
    }
}

if(!function_exists('get_permission_type')){
    function get_permission_type(){
        return [
            '0' => '导航类型',
            '1' => '带参链接',
            '2' => '细节控制'
        ];
    }
}

if(!function_exists("get_company_type")){
    function get_company_type(){
        return [
            '0' => 'B端',
            '1' => '城市运营',
            '2' => '商铺（工厂）',
            '3' => '展览中心',
        ];
    }
}

if(!function_exists('get_order_state')){
    /**
     * 好像只有财务的订单列表可以用 财务可以看全部状态的订单
     * @return array
     */
    function get_order_state(){
        return [
            "-1"=> "删除",
            "0" => "未审核",
            "1" => "订单有效",
            "2" => "审核不通过",
            "3" => "已经发货",
            "4" => "发货失败",
            "5" => "确认收货",
            "6" => "退货",
            "7" => "财务确认订单完成（可以结算）",
            "8" => "订单失效",
        ];
    }
}
if(!function_exists("get_order_state_map")){
    /**
     * 整体订单状态转移地图 好像现在用不着
     * @return array
     */
    function get_order_state_map(){
        return [
            0 =>[-1,1,2],
            1 =>[3,4],
            2 =>[0],
            3 =>[5,6],
            4 =>[2],
            5 =>[6,7],
            6 =>[8],
        ];
    }
}

if(!function_exists("get_order_state_accountant_dict")){
    /**
     * 财务订单状态转移地图
     * @return array
     */
    function get_order_state_accountant_dict(){
        return [-1,0,1,2,3,4,5,6,7,8];
    }
}

if(!function_exists("get_order_state_accountant_map")){
    /**
     * 财务订单状态转移地图
     * @return array
     */
    function get_order_state_accountant_map(){
        return [
            0 =>[1,2],
            4 =>[2],
            5 =>[7],
            6 =>[8],
        ];
    }
}

if(!function_exists("get_order_state_purchase_dict")){
    /**
     * 采购订单状态字典
     * @return array
     */
    function get_order_state_purchase_dict(){
        return [1, 3, 4, 5, 6,];
    }
}

if(!function_exists("get_order_state_purchase_map")){
    /**
     * 采购订单状态转移地图
     * @return array
     */
    function get_order_state_purchase_map(){
        return [
            1 =>[3,4],
            3 =>[5,6],
            5 =>[6],
        ];
    }
}


if(!function_exists('get_order_state_service_dict')){
    /**
     * 客服订单状态字典
     * @return array
     */
    function get_order_state_service_dict(){
        return [2];
    }
}

if(!function_exists('get_order_state_service_map')){
    /**
     * 客服订单状态转移地图
     * @return array
     */
    function get_order_state_service_map(){
        return [
            2 => [0]
        ];
    }
}


if(!function_exists('create_order_num')){
    function create_order_num(){
        //订单号码主体（YYYYMMDDHHIISSNNNNNNNN）
        $order_id_main = date('YmdHis') . rand(10000000,99999999);
        //订单号码主体长度
        $order_id_len = strlen($order_id_main);
        $order_id_sum = 0;
        for($i=0; $i<$order_id_len; $i++){
            $order_id_sum += (int)(substr($order_id_main,$i,1));
        }
        //唯一订单号码（YYYYMMDDHHIISSNNNNNNNNCC）
        $order_num = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100,2,'0',STR_PAD_LEFT);
        $res = db("order")->where('order_num',$order_num)->count();
        if($res){
            return create_order_num();
        }
        return $order_num;
    }
}


if(!function_exists('create_password')){
    function create_password($origin_password){
        $salt = 'ougongdeveloper';
        return md5($salt.$origin_password);
    }
}